#include <assert.h>
#include "pd_define.h"
#include "pd_log.h"
#include "pd_socket.h"
#include "pd_event.h"

int main()
{
    PD_LOG(WARN, "pd main");

    PdSocket *sock = pd_socket_init(PD_SOCK_TCP);
    int ret = pd_socket_set_addr(sock, "www.twitter.com", 80);
    assert(0 == ret);

    ret = pd_socket_reuse_addr(sock, 1);
    assert(0 == ret);
    ret = pd_socket_set_addr(sock, NULL, 8008);
    assert(0 == ret);
    ret = pd_socket_bind(sock);
    assert(0 == ret);
    ret = pd_socket_listen(sock, 1024);
    assert(0 == ret);

    PD_LOG(INFO, "addr=[%s]", pd_socket_str_addr(sock));
    PD_LOG(INFO, "peer=[%s]", pd_socket_str_peer(sock));

    ret = pd_socket_keep_alive(sock, 1);
    assert(0 == ret);

    ret = pd_socket_reuse_addr(sock, 1);
    assert(0 == ret);

    ret = pd_socket_linger(sock, 1, 10);
    assert(0 == ret);

    ret = pd_socket_tcp_nodelay(sock, 1);
    assert(0 == ret);

    ret = pd_socket_tcp_quickack(sock, 1);
    assert(0 == ret);

    ret = pd_socket_no_blocking(sock, 1);
    assert(0 == ret);

    ret = pd_socket_send_buffer(sock, 640000);
    assert(0 == ret);

    ret = pd_socket_recv_buffer(sock, 640000);
    assert(0 == ret);

    ret = pd_socket_send_timeo(sock, 1000);
    assert(0 == ret);

    ret = pd_socket_recv_timeo(sock, 1000);
    assert(0 == ret);

    PdSocketEvent *ev = pd_event_init();
    PdIOComponent ioc;
    ioc.sock = sock;
    ret = pd_event_add_event(ev, &ioc, 1, 0);
    assert(0 == ret);

    PdIOEvent event;
    ret = pd_event_get_events(ev, 10000000, &event, 1);
    assert(1 == ret);

    PdSocket *as = pd_socket_accept(event.ioc->sock);
    assert(NULL != as);
    PD_LOG(INFO, "addr=[%s]", pd_socket_str_addr(as));
    PD_LOG(INFO, "peer=[%s]", pd_socket_str_peer(as));

    PdIOComponent io1;
    io1.sock = as;
    ret = pd_event_add_event(ev, &io1, 1, 0);
    assert(0 == ret);
    ret = pd_event_get_events(ev, 10000000, &event, 1);
    assert(1 == ret);

    return 0;
}

